package com.yuzhitong.shapi.activity;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.base.BaseLayoutActivity;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.bean.MovieBean;
import com.yuzhitong.shapi.fragment.SearchMovieFragment;
import com.yuzhitong.shapi.fragment.SearchWebFragment;
import com.yuzhitong.shapi.util.AppUtil;
import com.yuzhitong.shapi.util.GlideUtil;
import com.yuzhitong.shapi.util.LoggerUtil;
import com.yuzhitong.shapi.util.UiUtil;
import com.yuzhitong.shapi.widget.ScoreView;

import java.util.ArrayList;

/**
 * Created by Brsya
 * CreateDate: 2022/5/9  12:42
 * Description:
 * 影视详情
 */
public class MovieDetailActivity extends BaseLayoutActivity {

    private CoordinatorLayout clContentPage;
    private LinearLayout pageBox;
    private RelativeLayout llPageTitleBox;
    private TextView tvPageTitle;
    private LinearLayout llTitleBackBox;
    private AppBarLayout appBarLayout;
    private ImageView ivMovieImg;
    private TextView tvMovieTitle;
    private ScoreView slScoreBox;
    private TextView tvMovieType;
    private TextView tvMovieInfo;
    private SlidingTabLayout tlMovieDetailSearchTitle;
    private ViewPager vpDetailSearchBox;


    private MovieBean movieBean;


    private String[] tabs = {"直搜", "必应", "神马", "百度", "头条"};
    private ArrayList<Fragment> fragments;
    private int totalScrollRange;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_detail);
        initView();
        initData();
    }

    private void initView() {
        llPageTitleBox = findViewById(R.id.ll_page_title_box);
        tvPageTitle = findViewById(R.id.tv_page_title);
        llTitleBackBox = findViewById(R.id.ll_title_back_box);
        ivMovieImg = findViewById(R.id.iv_movie_img);
        tvMovieTitle = findViewById(R.id.tv_movie_title);
        slScoreBox = findViewById(R.id.ll_score_box);
        tvMovieType = findViewById(R.id.tv_movie_type);
        tvMovieInfo = findViewById(R.id.tv_movie_info);
        tlMovieDetailSearchTitle = findViewById(R.id.tl_movie_detail_search_title);
        vpDetailSearchBox = findViewById(R.id.vp_detail_search_box);
        pageBox = findViewById(R.id.page_box);
        appBarLayout = findViewById(R.id.app_bar_layout);
        UiUtil.setStatusPadding(this, pageBox);
        UiUtil.setStatusDark(this, false);
        clContentPage = findViewById(R.id.cl_content_page);
    }

    private void initData() {

        if (getIntent() != null) {
            if (getIntent().getSerializableExtra(Contents.INTENT_MOVIE_DETAIL) != null) {
                if (getIntent().getSerializableExtra(Contents.INTENT_MOVIE_DETAIL) instanceof MovieBean) {
                    movieBean = (MovieBean) getIntent().getSerializableExtra(Contents.INTENT_MOVIE_DETAIL);
                }
            }
        }


        GlideUtil.loadBitmapColor(this, movieBean.getCoverUrl(), new GlideUtil.OnBitmapColorListener() {
            @Override
            public void onBitmapColor(int color) {
                color = color & 0xFF666666;
                int alphaColor = color & 0xAAFFFFFF;

                int[] colors = {alphaColor, color};
                GradientDrawable bg = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
                pageBox.setBackground(bg);
            }
        });
        tvPageTitle.setText(movieBean.getVideoType());
        GlideUtil.loadRound(ivMovieImg, movieBean.getCoverUrl(), 4);
        tvMovieTitle.setText(movieBean.getName());
        slScoreBox.setScore(movieBean.getDoubanScore());
        tvMovieType.setText(AppUtil.getMovieTagListStr(movieBean));

        tvMovieInfo.setText(movieBean.getVideoDescribe());

        fragments = new ArrayList<>();
        fragments.add(SearchMovieFragment.newInstance(movieBean.getName()));
        fragments.add(SearchWebFragment.newInstance(Contents.POSITION_MOVIE_BING, movieBean.getName()));
        fragments.add(SearchWebFragment.newInstance(Contents.POSITION_MOVIE_SHM, movieBean.getName()));
        fragments.add(SearchWebFragment.newInstance(Contents.POSITION_MOVIE_BD, movieBean.getName()));
        fragments.add(SearchWebFragment.newInstance(Contents.POSITION_MOVIE_TT, movieBean.getName()));
        vpDetailSearchBox.setOffscreenPageLimit(4);
        vpDetailSearchBox.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return tabs.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                totalScrollRange = appBarLayout.getTotalScrollRange();
                if (Math.abs(verticalOffset) >= totalScrollRange - 10) {
                    tvPageTitle.setText(movieBean.getName());
                }else{
                    tvPageTitle.setText(movieBean.getVideoType());
                }
            }
        });
        totalScrollRange = appBarLayout.getTotalScrollRange();
//        vpDetailSearchBox.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            private int selectPosition;
//            private int state;
//            private boolean isLeft;
//
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                LoggerUtil.e("positionOffset: " + positionOffset);
//                if (state == 1) {
//                    isLeft = selectPosition == 0;
//                }
//                if (position == 0) {
//                    CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
//                    if (behavior instanceof AppBarLayout.Behavior) {
//                        if (isLeft){
//                            if (((AppBarLayout.Behavior) behavior).getTopAndBottomOffset() == -totalScrollRange) {
//                                return;
//                            }
//                        }else{
//                            if (((AppBarLayout.Behavior) behavior).getTopAndBottomOffset() == 0) {
//                                return;
//                            }
//                        }
//                        ((AppBarLayout.Behavior) behavior).setTopAndBottomOffset((int) -(Math.min(positionOffsetPixels, totalScrollRange)));
//                    }
//                }
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                selectPosition = position;
//                if (state == 0) {
//                    if (selectPosition != 0) {
//                        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
//                        if (behavior instanceof AppBarLayout.Behavior) {
//                            ((AppBarLayout.Behavior) behavior).setTopAndBottomOffset(-totalScrollRange);
//                        }
//                    }
//                }
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//                // 0（END）,1(PRESS) , 2(UP) 。
//                this.state = state;
//                if (state == 0) {
//                    isLeft = selectPosition == 0;
//                }
//            }
//        });

        tlMovieDetailSearchTitle.setViewPager(vpDetailSearchBox);

        llTitleBackBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
